﻿/*
Copyright © 2010 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;
using System.IO;
using NUnit.Util;

namespace TestRunner
{
    /// <summary>
    /// Runner for all the tests of this package.
    /// </summary>
    public static class Runner
    {
        /// <summary>
        /// Search pattern used to find all the test package dlls.
        /// </summary>
        public const string TestPackageSearchPattern = "*-Test.dll";

        /// <summary>
        /// Name of the generated nunit project file.
        /// </summary>
        public const string NUnitProjectFile = "test-project.nunit";

        /// <summary>
        /// Runs the NUnit GUI for the current package.
        /// </summary>
        /// <returns>The error code of the application.</returns>
        [STAThread]
        public static int Main()
        {
            // Retrieve all the test packages.
            string[] files = Directory.GetFiles(
                Environment.CurrentDirectory,
                TestPackageSearchPattern,
                SearchOption.TopDirectoryOnly);

            // Create a NUnit configuration with all the associated test packages.
            ProjectService service = new ProjectService();
            try
            {
                NUnitProject project = service.NewProject();
                foreach (string file in files)
                {
                    project.ActiveConfig.Assemblies.Add(file);
                }

                project.Save(NUnitProjectFile);
            }
            finally
            {
                service.UnloadService();
            }

            string[] args = new string[2];
            args[0] = NUnitProjectFile;
            args[1] = "/run";

            return NUnit.Gui.AppEntry.Main(args);
        }
    }
}
